package com.aries.ui.widget.demo.module.status;

import com.aries.ui.helper.status.StatusViewHelper;
import com.aries.ui.widget.demo.ResourceTable;
import com.aries.ui.widget.demo.base.BaseAbilitySlice;
import com.aries.ui.widget.demo.util.LogUtil;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;

/**
 * @Author: AriesHoo on 2019/2/28 11:28
 * @E-Mail: AriesHoo@126.com
 * @Function: {@link StatusViewHelper} 使用示例
 * @Description:
 */
public class StatusViewHelperAbility extends BaseAbilitySlice implements Component.ClickedListener {
    Text mTvTitle;
    Switch mSBtnImmersible;
    Text mSTxtImmersible;
    Switch mSBtnTrans;
    Text mSTxtTrans;
    Switch mSBtnMargin;
    Text mSTxtMargin;
    Slider mSBarAlpha;
    Text mTvAlpha;
    Switch mSBtnPlus;
    Text mSTxtPlus;
    DirectionalLayout mLLayoutAlpha;

    private StatusViewHelper mStatusViewHelper;

    @Override
    protected void setTitleBar() {

    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
    }

    @Override
    protected int getLayout() {
        return ResourceTable.Layout_ability_status_view_helper;
    }

    @Override
    protected void initView(Intent intent) {

        mTvTitle = (Text) findComponentById(ResourceTable.Id_tv_titleStatus);
        mSBtnImmersible = (Switch) findComponentById(ResourceTable.Id_sBtn_immersibleStatus);
        mSTxtImmersible = (Text) findComponentById(ResourceTable.Id_sTxt_immersibleStatus);
        mSBtnTrans = (Switch) findComponentById(ResourceTable.Id_sBtn_transStatus);
        mSTxtTrans = (Text) findComponentById(ResourceTable.Id_sTxt_transStatus);
        mSBtnMargin = (Switch) findComponentById(ResourceTable.Id_sBtn_marginStatus);
        mSTxtMargin = (Text) findComponentById(ResourceTable.Id_sTxt_marginStatus);
        mSBarAlpha = (Slider) findComponentById(ResourceTable.Id_sBar_alphaStatus);
        mTvAlpha = (Text) findComponentById(ResourceTable.Id_tv_alphaStatus);
        mSBtnPlus = (Switch) findComponentById(ResourceTable.Id_sBtn_plusStatus);
        mSTxtPlus = (Text) findComponentById(ResourceTable.Id_sTxt_plusStatus);
        mLLayoutAlpha = (DirectionalLayout) findComponentById(ResourceTable.Id_lLayout_alphaStatus);

        mTvTitle.setText("StatusViewHelper示例");
        mTvTitle.setClickedListener(this::onClick);
        mSBtnImmersible.setCheckedStateChangedListener((absButton, isChecked) -> {
            mSTxtImmersible.setText(isChecked ? "沉浸" : "不沉浸");
            mSTxtTrans.setVisibility(isChecked ? Component.VISIBLE : Component.HIDE);
            mSBtnTrans.setVisibility(isChecked ? Component.VISIBLE : Component.HIDE);
            mSTxtMargin.setVisibility(isChecked ? Component.VISIBLE : Component.HIDE);
            mSBtnMargin.setVisibility(isChecked ? Component.VISIBLE : Component.HIDE);
            mSTxtPlus.setVisibility(isChecked ? Component.VISIBLE : Component.HIDE);
            mSBtnPlus.setVisibility(isChecked ? Component.VISIBLE : Component.HIDE);
            mLLayoutAlpha.setVisibility(mSBtnPlus.isChecked() && isChecked ? Component.VISIBLE : Component.HIDE);

            if(mSBtnTrans.isChecked() && mSBtnMargin.isChecked()){
                setStatusBase(Color.argb(255, 255, 255, 255));
            }else if(!mSBtnTrans.isChecked() && mSBtnMargin.isChecked()){
                setStatusBase(Color.argb(255, 95, 95, 95));
            }else if(!mSBtnTrans.isChecked() && !mSBtnMargin.isChecked()){
                setStatus(0x7F);
            }else if(mSBtnTrans.isChecked() && !mSBtnMargin.isChecked()){
                setStatus(0);
            }else{
                setStatus();
            }
        });
        mSBtnTrans.setCheckedStateChangedListener((absButton, isChecked) -> {
            mSTxtTrans.setText(isChecked ? "全透明" : "半透明");
            if(mSBtnMargin.isChecked()){
                if(isChecked){
                    setStatusBase(Color.argb(255, 255, 255, 255));
                }else{
                    setStatusBase(Color.argb(255, 95, 95, 95));
                }
            }else{
                if(isChecked){
                    setStatus(0);
                }else{
                    setStatus(0x7F);
                }
            }
        });
        mSBtnMargin.setCheckedStateChangedListener((absButton, isChecked) -> {
            mSTxtMargin.setText(isChecked ? "TopView 设置MarginTop" : "TopView 设置PaddingTop");
            if (isChecked) {
                if(mSBtnTrans.isChecked()){
                    setStatusBase(Color.argb(255, 255, 255, 255));
                }else{
                    setStatusBase(Color.argb(255, 95, 95, 95));
                }
            } else {
                if(mSBtnTrans.isChecked()){
                    setStatus(0);
                }else{
                    setStatus(0x7F);
                }
            }
        });
        mSBtnPlus.setCheckedStateChangedListener((absButton, isChecked) -> {
            mSTxtPlus.setText(isChecked ? "增加状态栏占位View" : "不增加状态栏占位View");
            mLLayoutAlpha.setVisibility(isChecked ? Component.VISIBLE : Component.HIDE);
            if (isChecked) {
                mSBtnTrans.setChecked(true);
            }
            mSTxtTrans.setVisibility(isChecked ? Component.HIDE : Component.VISIBLE);
            mSBtnTrans.setVisibility(isChecked ? Component.HIDE : Component.VISIBLE);
            mSTxtMargin.setVisibility(isChecked ? Component.HIDE : Component.VISIBLE);
            mSBtnMargin.setVisibility(isChecked ? Component.HIDE : Component.VISIBLE);
            if (isChecked) {
                setStatus(mSBarAlpha.getProgress());
            } else {
                if (mSBtnMargin.isChecked()) {
                    setStatusBase(Color.argb(255, 255, 255, 255));
                } else {
                    setStatus(0);
                }
            }
        });
        mSBarAlpha.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean fromUser) {
                mTvAlpha.setText("" + progress);
                setStatus();
            }

            @Override
            public void onTouchStart(Slider slider) {

            }

            @Override
            public void onTouchEnd(Slider slider) {

            }
        });
        mSBtnImmersible.setChecked(true);
        mSBtnTrans.setChecked(true);
        setStatus(0);
    }

    private void setStatus() {
        setStatus(mSBarAlpha.getProgress());
    }

    private void setStatus(int alphaValue) {
        setStatusBase(Color.argb(alphaValue, 0, 0, 0));
    }

    private void setStatusBase(int defaultAlpha) {
        if (mStatusViewHelper == null) {
            mStatusViewHelper = StatusViewHelper.with(this.getAbility(), mContentView);
        }
        mStatusViewHelper.setTopView(mTvTitle, mSBtnMargin.isChecked())
                .setControlEnable(mSBtnImmersible.isChecked())
                .setTransEnable(mSBtnTrans.isChecked())
                .setLogEnable(true)
                .setPlusStatusViewEnable(mLLayoutAlpha.getVisibility() == Component.VISIBLE ? mSBtnPlus.isChecked() : true)
                .setStatusLayoutDrawable(mTvTitle.getBackgroundElement())
                .setStatusViewColor(defaultAlpha)
                .init(mContentView);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_tv_titleStatus:
                terminate();
                break;
        }
    }

    @Override
    protected void onStop() {
        mStatusViewHelper.destroy();
        mStatusViewHelper = null;
        super.onStop();
    }
}
